<?php
declare(strict_types=1);
/**
 * @project: 兔兔找图微信小程序
 * @author: kert
 * @date: 2023/5/16
 * @link: https://www.tutudati.com/
 * @site: 微信搜索-兔兔找图
 */
namespace App\Logic\User\Repository\Image;

use App\Logic\User\Repository\UserRepositoryInterface;
use App\Models\User\Image\ImageModel;
use App\Models\User\Image\ImageShare;
use Closure;
use Illuminate\Support\Facades\DB;

class ImageShareRepository implements UserRepositoryInterface
{

    /**
     * 用户中心分享列表
     * @param Closure $closure
     * @param int $perSize
     * @param array $searchFields
     * @return array
     */
    public function repositorySelect(Closure $closure, int $perSize, array $searchFields = []): array
    {
        $items = ImageShare::query()
            ->with(["image:uid,device_type,title,url,path"])
            ->whereHas("image")
            ->where($closure)
            ->orderByDesc("id")
            ->groupBy("image_uid")
            ->paginate($perSize, $searchFields);

        return [
            "items" => $items->items(),
            "page"  => $items->currentPage(),
            "size"  => $perSize,
            "total" => $items->total(),
        ];
    }

    public function repositoryCreate(array $insertInfo): bool
    {
        $authorInfo = ImageModel::query()->where("uid", "=", $insertInfo["image_uid"])->first(["author_uid"]);
        if (empty($authorInfo)) {
            return false;
        }
        $authorInfo = $authorInfo->toArray();
        $insertInfo["author_uid"] = $authorInfo["author_uid"];
        $model = ImageShare::query()->create($insertInfo);
        return !empty($model->getKey());
    }

    /**
     * 查询创作者中心收藏列表
     * @param Closure $closure
     * @param int $perSize
     * @param array $searchFields
     * @return array
     */
    public function repositoryAuthorSelect(Closure $closure, int $perSize, array $searchFields = []): array
    {
        $items = ImageShare::query()
            ->with(["user:nickname,avatar_url,uid"])
            ->with(["image:uid,device_type,title"])
            ->where($closure)
            ->where("user_uid", "!=", 0)
            ->groupBy("user_uid")
            ->orderByDesc("id")
            ->paginate($perSize, $searchFields);

        return [
            "items" => $items->items(),
            "page"  => $items->currentPage(),
            "size"  => $perSize,
            "total" => $items->total(),
        ];
    }

    public function repositoryFind(Closure $closure, array $searchFields = []): array
    {
        $bean = ImageShare::query()->where($closure)->first($searchFields);
        return !empty($bean) ? $bean->toArray() : [];
    }

    public function repositoryUpdate(array $updateWhere, array $updateInfo): int
    {
        return 0;
    }

    public function repositoryWhereInDelete(array $deleteWhere, string $field): int
    {
        return 0;
    }

    public function repositoryDelete(array $deleteWhere): int
    {
        return 0;
    }

    /**
     * 用户分享总数
     * @param array $searchWhere
     * @return int
     */
    public function repositoryUserTotal(array $searchWhere): int
    {
        return ImageShare::query()->where($searchWhere)->orderByDesc("id")->count(DB::raw('DISTINCT image_uid'));
    }
}
